﻿using Microsoft.OpenApi.Models;

namespace SP.User.WebApi.Config
{
    public static class SwaggerConfig
    {
        public static void SwaggerSetup(this IServiceCollection Services)
        {
            Services.AddSwaggerGen(
                 s =>    //swagger开发者信息
                 {
                     s.SwaggerDoc("v1", new OpenApiInfo()
                     {
                         Version = "v1",
                         Title = "swagger测试项目",
                         Description = "接口文档说明",
                         Contact = new OpenApiContact()
                         {
                             Name = "zhusongpeng",
                             Email = "666666@qq.com",
                             Url = null
                         }
                     });
                     //s.EnableAnnotations();
                     //如何在swagger上显示中文注释呢
                     //读取SP.User.WebApi.xml文件里面的注释信息，电视在swagger界面上
                     //string xmlPath = "D:\内网通\物联网\专高六\作业+日考\iot6-2005A\SP.User\SP.User.WebApi\bin\Debug\net6.0\\SP.User.WebApi.xml";
                     string xmlPath = AppDomain.CurrentDomain.BaseDirectory + "SP.User.WebApi.xml ";
                      s.IncludeXmlComments(xmlPath,true);  //swagger显示注释
                     //开启Authorize权限按钮
                     s.AddSecurityDefinition("JWTBearer", new OpenApiSecurityScheme() { Description = "这是方式一(直接在输入框中输入认证信息，不需要在开头添加Bearer) ", Name = "Authorization",
                         //jwt默认的参数名称
                         In = ParameterLocation.Header,
                         //jwt默认存放Authorization信息的位置(请求头 中)
                         Type = SecuritySchemeType.Http, 
                         Scheme = "Bearer" });
                     //声明一个Scheme，注意下面的Id要和上面AddSecurityDefinition中的参数name一致
                     var scheme = new OpenApiSecurityScheme { Reference = new OpenApiReference() { Id = "JWTBearer",
                         //这个名字与上面的一样
                         Type = ReferenceType.SecurityScheme } };//注册全局认证（所有的接口都可以使用认证）
                     s.AddSecurityRequirement(new OpenApiSecurityRequirement { { scheme, Array.Empty<string>() } });
                 }
                );
        }
    }
}
